import router from '@/router'
import store from '@/store'

const whitelist = ['/login', '/404']
// 前置路由守卫
router.beforeEach(async(to, from, next) => {
  // 先判断是否处于登录状态
  if (store.getters.token) {
    if (!store.getters.userId) {
      await store.dispatch('user/getUserInfoAction')
      store.dispatch('permission/getRealRoutes', [...store.getters.menus])
      next(to.path)
    }
    // 看是否是去登录界面
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    // 看是否处于白名单
    if (whitelist.includes(to.path)) {
      // 处于白名单放行
      next()
    } else {
      // 否则让去login界面登录
      next('/login')
    }
  }
}
)
